package org.come.summonequip;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.math.BigDecimal;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.come.model.Shop;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.UserMessUntil;

import com.tool.btn.FormsOnOffBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;

/**
 * 兑换奖励主面板
 * <p>
 * Title : JpanelCashRewardsMain
 * </p>
 *
 * @author : HGC
 * @date : 2019年7月22日 下午4:36:58
 * @version : 1.0.0
 */
public class WpanelCashRewardsMain extends JPanel {

    /**
     * 12个格子goodsLabs<br>
     * 2个消耗展示consumptionGoodsLabs<br>
     */
    private JLabel[] goodsLabs, consumptionGoodsLabs;
    /**
     * 选中的物品chooseGoods<br>
     */
    private JLabel chooseGoods;

    /**
     * 输入兑换数量textNum<br>
     */
    private JTextField textNum;

    /**
     * 兑换按钮
     */
    private BtnSummonEquipMain exchangeBtn;

    /** 选中的物品 */
    private Shop chooseShop;
    private BigDecimal shopPrice;

    /**
     * 召唤兽装备奖励兑换1<br>
     * 星卡奖励兑换2<br>
     */
    private int goodsType = -1;

    public WpanelCashRewardsMain() {
        setPreferredSize(new Dimension(356, 362));
        setOpaque(false);
        setLayout(null);

        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/1/s74.png", 1, 92);
        offBtn.setBounds(331, 0, 23, 23);
        this.add(offBtn);
        /** 添加组件 */
        getGoodsLabs();
        getChooseGoods();
        getExchangeBtn();
        // getConsumptionGoodsLabs();
        getTextNum();
    }

    /**
     * 召唤兽装备
     *
     * @param g
     */
    public void showShop(Graphics g) {
        List<Shop> npcshop = UserMessUntil.getNpcshop().getNpcShopMap().get("600");
        if (npcshop == null) {
            return;
        }
        for (int i = 0; i < npcshop.size(); i++) {
            g.drawImage(GoodsListFromServerUntil.imgpath(npcshop.get(i).getShopskin()).getImage(), 49 + i % 6 * 51,56 + i / 6 * 51, 49, 49, null);
            if (i >= 11) {
                return;
            }
        }

    }

    /**
     * 星卡
     *
     * @param g
     */
    public void showStarCard(Graphics g) {
        List<Shop> npcshop = UserMessUntil.getNpcshop().getNpcShopMap().get("601");
        if (npcshop == null) {
            return;
        }
        for (int i = 0; i < npcshop.size(); i++) {
            g.drawImage(GoodsListFromServerUntil.imgpath(npcshop.get(i).getShopskin()).getImage(), 22 + i % 6 * 51,57 + i / 6 * 51, 49, 49, null);
            if (i >= 11) {
                return;
            }
        }

    }

    private ImageIcon iconBack;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (iconBack == null) {
            iconBack = CutButtonImage.getImage("inkImg/hongmu/2/兑换奖励_底板_w356,h362.png", 356, 362);
        }
        g.drawImage(iconBack.getImage(), 0, 0, 356, 362, this);
        if (goodsType == 1) {
            if (chooseShop != null) {
                if (shopPrice != null) {
                    g.setColor(Color.white);
                    g.setFont(UIUtils.TEXT_FONT1);
                    g.drawString(
                            "奖章:" + Integer.parseInt(textNum.getText()) * chooseShop.getShopprice()
                                    + "/" + RoleData.getRoleData().getLoginResult().getScoretype("比斗奖章"), 182, 235);
                }
            }
            showShop(g);
            g.setColor(UIUtils.COLOR_NAME16);
            g.setFont(UIUtils.TEXT_FONT1);
            g.drawString("拥有比斗奖章:" + RoleData.getRoleData().getLoginResult().getScoretype("比斗奖章"), 20, 330);
        } else if (goodsType == 2) {
            if (chooseShop != null) {
                if (shopPrice != null) {
                    g.setColor(Color.white);
                    g.setFont(UIUtils.TEXT_FONT1);
                    g.drawString(
                            "星芒:" + Integer.parseInt(textNum.getText()) * chooseShop.getShopprice()
                                    + "/" + RoleData.getRoleData().getLoginResult().getScoretype("星芒"), 182, 235);
                }
            }
            showStarCard(g);
        }
    }

    public ImageIcon getIconBack() {
        return iconBack;
    }

    public void setIconBack(ImageIcon iconBack) {
        this.iconBack = iconBack;
    }

    public JLabel[] getGoodsLabs() {
        if (goodsLabs == null) {
            goodsLabs = new JLabel[12];
            for (int i = 0; i < goodsLabs.length; i++) {
                goodsLabs[i] = new JLabel();
                goodsLabs[i].setBounds(22 + i % 6 * 51, 57 + i / 6 * 51, 50, 50);
                // goodsLabs[i].setOpaque(true);
              //  goodsLabs[i].addMouseListener(new MouseListenerCashRewards(i, this));
                this.add(goodsLabs[i]);
            }
        }
        return goodsLabs;
    }

    public void setGoodsLabs(JLabel[] goodsLabs) {
        this.goodsLabs = goodsLabs;
    }

    public JLabel getChooseGoods() {
        if (chooseGoods == null) {
            chooseGoods = new JLabel();
            chooseGoods.setBounds(55, 192, 55, 53);
            // chooseGoods.setOpaque(true);
            this.add(chooseGoods);
        }
        return chooseGoods;
    }

    public void setChooseGoods(JLabel chooseGoods) {
        this.chooseGoods = chooseGoods;
    }

    public JTextField getTextNum() {
        if (textNum == null) {
            textNum = new JTextField("0");
            textNum.setForeground(Color.white);
            textNum.setFont(UIUtils.TEXT_FONT);
            textNum.setBounds(185, 201, 48, 16);
            textNum.setOpaque(false);
            textNum.setCaretColor(Color.white);
            textNum.setBorder(null);
            this.add(textNum);
            textNum.getDocument().addDocumentListener(new DocumentListener() {
                @Override
                public void removeUpdate(DocumentEvent e) {
                    String s = textNum.getText();
                    try {
                        int sum = Integer.parseInt(s);
                        if (chooseShop != null) {
                            shopPrice = new BigDecimal(sum * chooseShop.getShopprice());
                        }
                    } catch (NumberFormatException e1) {
                    }
                }

                @Override
                public void insertUpdate(DocumentEvent e) {
                    String s = textNum.getText();
                    try {
                        int sum = Integer.parseInt(s);
                        if (chooseShop != null) {
                            shopPrice = new BigDecimal(sum * chooseShop.getShopprice());
                        }
                    } catch (NumberFormatException e1) {
                    }

                }

                @Override
                public void changedUpdate(DocumentEvent e) {
                }
            });
            textNum.addKeyListener(new KeyListener() {
                @Override
                public void keyTyped(KeyEvent e) {
                    int charstr = e.getKeyChar();
                    String str = textNum.getText();
                    if (str.length() == 0) {
                        textNum.setText(0 + "");
                    }

                    // control the value between 0 and 9
                    if (charstr < KeyEvent.VK_0 || charstr > KeyEvent.VK_9) {
                        e.consume();
                    }
                    if (str.length() == 1) {
                        if ("0".equals(str)) {
                            textNum.setText("");
                        }
                    }
                    if (str.length() > 0) {
                        if (str.length() > 1) {
                            e.consume();
                            textNum.setText(99 + "");
                            int sum = Integer.parseInt(textNum.getText());
                            if (chooseShop != null) {
                                shopPrice = new BigDecimal(sum * chooseShop.getShopprice());
                            }
                        }
                    }
                }

                @Override
                public void keyPressed(KeyEvent e) {
                }

                @Override
                public void keyReleased(KeyEvent e) {
                }
            });
        }
        return textNum;
    }

    public void setTextNum(JTextField textNum) {
        this.textNum = textNum;
    }

    public BtnSummonEquipMain getExchangeBtn() {
        if (exchangeBtn == null) {
            //exchangeBtn = new BtnSummonEquipMain("inkImg/hongmu/1/6026.png", 1, UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY88,
                    //"兑换", 30, this);
            exchangeBtn.setBounds(134, 271, 60, 26);
            this.add(exchangeBtn);
        }
        return exchangeBtn;
    }

    public void setExchangeBtn(BtnSummonEquipMain exchangeBtn) {
        this.exchangeBtn = exchangeBtn;
    }

    public JLabel[] getConsumptionGoodsLabs() {
        if (consumptionGoodsLabs == null) {
            consumptionGoodsLabs = new JLabel[2];
            for (int i = 0; i < consumptionGoodsLabs.length; i++) {
                consumptionGoodsLabs[i] = new JLabel("奖章:" + i * 60 + "/" + 12354 * i);
                consumptionGoodsLabs[i].setForeground(Color.white);
                consumptionGoodsLabs[i].setFont(UIUtils.TEXT_FONT);
                consumptionGoodsLabs[i].setBounds(182, 226 + i * 22, 120, 20);

                this.add(consumptionGoodsLabs[i]);
            }
        }
        return consumptionGoodsLabs;
    }

    public void setConsumptionGoodsLabs(JLabel[] consumptionGoodsLabs) {
        this.consumptionGoodsLabs = consumptionGoodsLabs;
    }

    public Shop getChooseShop() {
        return chooseShop;
    }

    public void setChooseShop(Shop chooseShop) {
        this.chooseShop = chooseShop;
    }

    public BigDecimal getShopPrice() {
        return shopPrice;
    }

    public void setShopPrice(BigDecimal shopPrice) {
        this.shopPrice = shopPrice;
    }

    public int getGoodsType() {
        return goodsType;
    }

    public void setGoodsType(int goodsType) {
        this.goodsType = goodsType;
    }

}
